import { clearToken, setToken } from "../../utils/token"
import { http } from "../../utils/http"
// import axios from "axios"
// 登录
export const LoginPackages = (key) => async (dispatch) => {
  try {
    const res = await http.post(`/authorizations`, key)
    const {
      message,
      data: { token }
    } = res.data
    if (message === "OK") {
      dispatch({
        type: "SET_TOKEN",
        payload: token
      })
      // 保存token到localStorage中
      setToken(token)
    }
  } catch (error) {
    console.log(error)
  }
}
// 退出登录
export const logout = () => {
  return (dispatch) => {
    // 清楚redux中token
    dispatch({ type: "CLEAR_TOKEN" })
    // 清楚localstorage
    clearToken()
  }
}
